﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;
using Antu.Core.DataAccess;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZS_PGBMXXControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        TextBox txt项目名称, txt征收范围, txt需征收房屋面积, txt被征收居民户数, txt工企单位数, txtPNID, txtID, txtSID, 报名资质;
        MPCalendar txt报名信息发布日期;
        Button btn_Insert, btnReturn;
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            InitControl();
            btn_Insert.Click += new EventHandler(btn_Insert_Click);
            btnReturn.Click += new EventHandler(btnReturn_Click);
            string strpnid = base.Page.Request["PNID"];
            string strid = base.Page.Request["ID"];
            string strsid = base.Page.Request["SID"];
            if (!base.Page.IsPostBack)
            {
                if (!string.IsNullOrEmpty(strsid)) { txtSID.Text = strsid; }
                if (!string.IsNullOrEmpty(strpnid))
                {
                    txtPNID.Text = strpnid;
                    string strpid = _EluZSDataProvider.GetSomeStr("zs_prono", "pid", "id", strpnid);
                    EluZSProvider.Project project = _EluZSDataProvider.GetProjectXX(Page.Request["pnid"]);
                    txt项目名称.Text = project.项目名称;
                    txt征收范围.Text = project.征收范围;
                }
                if (!string.IsNullOrEmpty(strsid))
                {
                    txtID.Text = strid;
                    //EluZSProvider.Pgbmfbxx pgbmfbxx = GetPGJGBMXX("0", Page.Request["pnid"],"", "");
                    //txt需征收房屋面积.Text = pgbmfbxx.需征收房屋面积;
                    //txt被征收居民户数.Text = pgbmfbxx.被征收居民户数;
                    //txt工企单位数.Text = pgbmfbxx.工企单位数;
                    DataTable dt = GetPGJGBMXX("0", Page.Request["pnid"], "", "");
                    txt需征收房屋面积.Text = dt.Rows[0]["需征收房屋面积"].ToString();
                    txt被征收居民户数.Text = dt.Rows[0]["被征收居民户数"].ToString();
                    txt工企单位数.Text = dt.Rows[0]["工企单位数"].ToString();

                    //if (pgbmfbxx.报名信息发布日期.HasValue) { txt报名信息发布日期.Text = pgbmfbxx.报名信息发布日期.Value.ToShortDateString(); }
                    txt项目名称.Text = dt.Rows[0]["项目名称"].ToString();
                    txt征收范围.Text = dt.Rows[0]["征收范围"].ToString();
                    txtPNID.Text = dt.Rows[0]["pnid"].ToString();
                    txt报名信息发布日期.Text = dt.Rows[0]["报名信息发布日期"].ToString();
                    //txtSID.Text = pgbmfbxx.SID;
                }
                else { txtID.Text = System.Guid.NewGuid().ToString().Replace("-", ""); }
                string tjzt = _EluZSDataProvider.GetSomeStr("zs_pgfbsq", "提交状态", "id", strsid);
                if (tjzt == "1") { base.Page.ClientScript.RegisterStartupScript(this.GetType(), "displaysave", "<script>displaysave();</script>"); }
            }
            

            getzizhi();
            if (base.Page.Request["flag"] == "pgjg")
            {
                btn_Insert.Visible = false;
                base.Page.ClientScript.RegisterStartupScript(this.GetType(), "23232323", "<script>javascript:document.getElementById('btn_daying').style.display='none';</script>");
            }

        }


        protected void getzizhi() 
        {
            string sql = "Select * from zs_pgfbsq where pnid = '"+base.Page.Request["PNID"]+"'";
            报名资质 = (TextBox)FindControl("报名资质");
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            if (dt.Rows.Count>0)
	        {
                报名资质.Text = dt.Rows[0]["资质"].ToString();
	        }
                    
        }



        public DataTable GetPGJGBMXX(string tt, string pnid, string strwhere1, string strwhere2)
        {
            string sql = string.Empty;
            if (tt == "1")
            {
                sql = string.Format(@"select t.*,to_char(t.申请时间,'yyyy-mm-dd') 申请时间,
(select 立项号 from zs_project where id in t.pnid) 立项号,
(select 项目名称 from zs_project where id in t.pnid) 项目名称,
(select 征收范围 from zs_project where id in t.pnid) 征收范围,
t.提交状态,{0} 
(select id bmid from ZS_PGBMFBXX where sid=t.id) bmid,
(select 报名信息发布日期 bmid from ZS_PGBMFBXX where sid=t.id) 报名信息发布日期      
from zs_pgfbsq t where 提交状态=1 and pnid ='{1}'", strwhere1, pnid);
            }
            else if (tt == "0")
            {
                sql = string.Format(@"select t.*,to_char(t.申请时间,'yyyy-mm-dd') 申请时间,
(select 立项号 from zs_project where id in t.pnid) 立项号,
(select 项目名称 from zs_project where id in t.pnid) 项目名称,
(select 征收范围 from zs_project where id in t.pnid) 征收范围,
t.提交状态,{0} 
(select id bmid from ZS_PGBMFBXX where sid=t.id) bmid,
(select 报名信息发布日期 bmid from ZS_PGBMFBXX where sid=t.id) 报名信息发布日期    
from zs_pgfbsq t where pnid ='{1}'", strwhere1, pnid);
            }
            if (!string.IsNullOrEmpty(strwhere2)) { sql += strwhere2; }
            sql += " order by t.申请时间 desc";
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            return dt;
        }



        protected void InitControl()
        {
            btn_Insert = (Button)FindControl("btn_Insert");
            btnReturn = (Button)FindControl("btnReturn");
            txt项目名称 = (TextBox)FindControl("txt项目名称");
            txt征收范围 = (TextBox)FindControl("txt征收范围");
            txt需征收房屋面积 = (TextBox)FindControl("txt需征收房屋面积");
            txt被征收居民户数 = (TextBox)FindControl("txt被征收居民户数");
            txt工企单位数 = (TextBox)FindControl("txt工企单位数");
            txt报名信息发布日期 = (MPCalendar)FindControl("txt报名信息发布日期");
            txtPNID = (TextBox)FindControl("txtPNID");
            txtID = (TextBox)FindControl("txtID");
            txtSID = (TextBox)FindControl("txtSID");
        }

        protected void btn_Insert_Click(object sender, EventArgs e)
        {
            string strid = base.Page.Request["ID"];
            EluZSProvider.Pgbmfbxx pgbmfbxx = new EluZSProvider.Pgbmfbxx();
            pgbmfbxx.PNID = txtPNID.Text;
            pgbmfbxx.SID = txtSID.Text;
            pgbmfbxx.需征收房屋面积 = txt需征收房屋面积.Text.Trim();
            pgbmfbxx.被征收居民户数 = txt被征收居民户数.Text.Trim();
            pgbmfbxx.工企单位数 = txt工企单位数.Text.Trim();
            pgbmfbxx.项目名称 = txt项目名称.Text.Trim();
            pgbmfbxx.征收范围 = txt征收范围.Text.Trim();
            if (string.IsNullOrEmpty(txt报名信息发布日期.Text))
            {
                txt报名信息发布日期.Text = System.DateTime.Now.ToString();
            }
            pgbmfbxx.报名信息发布日期 = Convert.ToDateTime(txt报名信息发布日期.Text);
            bool save = _EluZSDataProvider.SavePGJGBMXX(strid, pgbmfbxx);
            string sid = base.Page.Request["SID"];
            string sql = "update zs_pgfbsq set 提交状态 = 1 where pnid = '" + Page.Request["pnid"] + "'";
            _EluZSDataProvider.RunSqlNon(sql);
            if (save) { base.Page.Response.Write("<script>alert('保存成功！');window.location.href('ZS_PGFBSQ.aspx?pnid=" + base.Page.Request["PNID"] + "');</script>"); }
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            string strflag = base.Page.Request["flag"];
            string strPnid = base.Page.Request["PNID"];
            if (!string.IsNullOrEmpty(strflag) && strflag == "pgjg")
            {
                base.Page.Response.Redirect("ZS_PGJGBMInfo.aspx?PNID=" + strPnid);
            }
            else {
                base.Page.Response.Redirect("ZS_PGFBSQ.aspx?PNID=" + strPnid);
            }
        }
    }
}


